package com.study.map.service.impl;

import com.alibaba.fastjson.JSON;
import com.study.map.config.GaodeMapConfig;
import com.study.map.constant.GaodeMapConstants;
import com.study.map.dto.ReGeoRequest;
import com.study.map.dto.ReGeoResponse;
import com.study.map.exception.MapServiceException;
import com.study.map.service.IReGeoService;
import com.study.map.util.MapHttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 逆地理编码服务实现类
 *
 * @author study
 * @date 2025-08-31
 */
@Slf4j
@Service
public class ReGeoServiceImpl implements IReGeoService {

    @Autowired
    private GaodeMapConfig goodeMapConfig;

    @Autowired
    private MapHttpUtil mapHttpUtil;

    /**
     * 逆地理编码：将经纬度坐标转换为地址
     *
     * @param request 逆地理编码请求参数
     * @return 逆地理编码结果
     * @throws MapServiceException 地图服务异常
     */
    @Override
    public ReGeoResponse reGeoCode(ReGeoRequest request) throws MapServiceException {
        try {
            // 构建请求URL
            String url = goodeMapConfig.getApiUrl() + "/" + GaodeMapConstants.API_VERSION_V3 + GaodeMapConstants.RE_GEO_CODE_URL;

            // 构建请求参数
            Map<String, Object> params = new HashMap<>();
            if (StringUtils.hasText(request.getLocation())) {
                params.put("location", request.getLocation());
            }
            params.put("poitype", request.getPoitype());
            params.put("output", request.getOutput());
            params.put("roadlevel", request.getRoadlevel());
            params.put("coordtype", request.getCoordtype());

            // 发送HTTP请求
            ReGeoResponse response = mapHttpUtil.doGet(url, params, ReGeoResponse.class);

            // 解析响应结果
//            ReGeoResponse response = JSON.parseObject(responseStr, ReGeoResponse.class);

            // 检查响应状态
            if (!GaodeMapConstants.STATUS_SUCCESS.equals(response.getStatus())) {
                log.error("逆地理编码请求失败，状态码: {}, 信息: {}", response.getStatus(), response.getInfo());
                throw new MapServiceException("逆地理编码请求失败: " + response.getInfo());
            }

            return response;
        } catch (Exception e) {
            log.error("逆地理编码请求异常", e);
            throw new MapServiceException("逆地理编码请求异常", e);
        }
    }

    /**
     * 逆地理编码：将经纬度坐标转换为地址
     *
     * @param location 经纬度坐标，格式：经度,纬度
     * @return 逆地理编码结果
     * @throws MapServiceException 地图服务异常
     */
    @Override
    public ReGeoResponse reGeoCode(String location) throws MapServiceException {
        ReGeoRequest request = new ReGeoRequest();
        request.setLocation(location);
        return reGeoCode(request);
    }

    /**
     * 逆地理编码：将经纬度坐标转换为地址
     *
     * @param longitude 经度
     * @param latitude 纬度
     * @return 逆地理编码结果
     * @throws MapServiceException 地图服务异常
     */
    @Override
    public ReGeoResponse reGeoCode(String longitude, String latitude) throws MapServiceException {
        ReGeoRequest request = new ReGeoRequest();
        request.setLocation(longitude + "," + latitude);
        return reGeoCode(request);
    }
}